<?php

class Blogmodel extends Model
{
		//Constructor
		function Blogmodel()
		{
			parent::Model();

		}
		
		function newEntry($userId)
		{
			$blog_name = $this->input->post('blog_name');
			$blog_content = $this->input->post('blog_content');
			$datetime = date('Y-m-d H:i:s');
			
			if($userId == ''){ $userId = $this->session->userdata('userId');}
			
			$this->db->set('blog_name', $blog_name);
			$this->db->set('blog_content', $blog_content);
			$this->db->set('blog_date', $datetime);
			$this->db->set('blog_owner', $userId);
			$this->db->insert('blog');
			
			$this->session->set_flashdata('success',  'บันทึกข้อมูลเรียบร้อยแล้ว');
			redirect('profile/blog');
		}
		
		function getBlog($userId, $limit, $blogId='')
		{
			if($blogId == ''){
			$orderBy ='blog_id DESC';
			$this->db->select('*');
			$this->db->where('blog_owner', $userId);
			$this->db->order_by($orderBy);
			$this->db->limit($limit);
			$query = $this->db->get('blog');
			
			if($query->num_rows() > 0)
			{
				foreach($query->result_array() as $row)
				{
					$blog[] = $row;
				}
				return $blog;
			}
			else{
				return false;
			}
			}else{
			$this->db->select('*');
			$this->db->where('blog_owner', $userId);
			$this->db->where('blog_id', $blogId);
			$query = $this->db->get('blog');
			
			if($query->num_rows() > 0)
			{
				foreach($query->result_array() as $row)
				{
					$blog[] = $row;
				}
				return $blog;
			}
			else{
				return false;
			}
			}
		}

		function getLastBlog($limit)
		{
			$orderBy ='blog_id DESC';
			$this->db->select('*');
			$this->db->order_by($orderBy);
			$this->db->limit($limit);
			$query = $this->db->get('blog');

			if($query->num_rows() > 0)
			{
				foreach($query->result_array() as $row)
				{
					$lastBlog[] = $row;
				}
				return $lastBlog;
			}
			else{
				return false;
			}
		}
		
		function getBlogComment($blogId, $limit)
		{
			$orderBy ='blog_comm_id ASC';
			$this->db->select('*');
			$this->db->where('ref_blog_id', $blogId);
			$this->db->order_by($orderBy);
			$this->db->limit($limit);
			$query = $this->db->get('blog_comment');
			
			if($query->num_rows() > 0)
			{
				foreach($query->result_array() as $row)
				{
					$blogComment[] = $row;
				}
				return $blogComment;
			}
			else{
				return false;
			}
		}

		function getLastComment($userId, $limit)
		{
			$orderBy ='blog_comm_date DESC';
			$this->db->select('*');
			//$this->db->where('ref_user_id', $userId);
			$this->db->order_by($orderBy);
			$this->db->limit($limit);
			$query = $this->db->get('blog_comment');

			if($query->num_rows() > 0)
			{
				foreach($query->result_array() as $row)
				{
					$lastComment[] = $row;
				}
				return $lastComment;
			}
			else{
				return false;
			}

		}

		function blogCount($blog_id)
		{
			$this->db->select('blog_view');
			$this->db->where('blog_id', $blog_id);
			$query = $this->db->get('blog');
			foreach ($query->result() as $row)
			{
				$view = $row->blog_view;
			}
			$total = $view + 1;
			$this->db->where('blog_id', $blog_id);
			$this->db->update('blog', array('blog_view' => $total));
		}
		
		function blogComment($userId)
		{
			if($userId == ''){ $userId = $this->session->userdata('userId');}
			
			$datetime = date('Y-m-d H:i:s');
			$blog_id = $this->input->post('blog_id');
			$blog_comment = $this->input->post('blog_comment');
			
			$this->db->set('blog_comm_content', $blog_comment);
			$this->db->set('blog_comm_date', $datetime);
			$this->db->set('ref_user_id', $userId);
			$this->db->set('ref_blog_id', $blog_id);
			$this->db->insert('blog_comment');
		}

		function getBlogOwner($blogId)
		{
			$this->db->select('blog_owner');
			$this->db->where('blog_id', $blogId);
			$query = $this->db->get('blog');
			if($query->num_rows() > 0)
			{
				{
					foreach($query->result() as $row)
					$blogOwner = $row->blog_owner;
				}
				return $blogOwner;
			}
			else
				{
					return false;
				}
		}
		
}